/*     */ package com.zimbra.cs.service.admin;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AdminConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.NamedEntry;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.SearchAccountsOptions;
/*     */ import com.zimbra.cs.account.SearchDirectoryOptions.SortOpt;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.accesscontrol.AdminRight;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class FixCalendarEndTime
/*     */   extends AdminDocumentHandler
/*     */ {
/*     */   public static final String ALL = "all";
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  44 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*     */     
/*     */ 
/*     */ 
/*  48 */     checkRight(zsc, context, null, AdminRight.PR_SYSTEM_ADMIN_ONLY);
/*     */     
/*  50 */     boolean sync = request.getAttributeBool("sync", false);
/*  51 */     List<Element> acctElems = request.listElements("account");
/*  52 */     List<String> acctNames = parseAccountNames(acctElems);
/*  53 */     if (acctNames.isEmpty())
/*  54 */       throw ServiceException.INVALID_REQUEST("Accounts must be specified", null);
/*  55 */     if (sync) {
/*  56 */       fixAccounts(acctNames);
/*     */     } else {
/*  58 */       CalendarEndTimeFixupThread thread = new CalendarEndTimeFixupThread(acctNames);
/*     */       
/*  60 */       thread.start();
/*     */     }
/*     */     
/*  63 */     Element response = zsc.createElement(AdminConstants.FIX_CALENDAR_END_TIME_RESPONSE);
/*  64 */     return response;
/*     */   }
/*     */   
/*     */   protected List<String> parseAccountNames(List<Element> acctElems) throws ServiceException {
/*  68 */     List<String> a = new ArrayList(acctElems.size());
/*  69 */     for (Element elem : acctElems) {
/*  70 */       String name = elem.getAttribute("name");
/*  71 */       if ("all".equals(name)) {
/*  72 */         List<String> all = new ArrayList(1);
/*  73 */         all.add("all");
/*  74 */         return all;
/*     */       }
/*  76 */       String[] parts = name.split("@");
/*  77 */       if (parts.length != 2) {
/*  78 */         throw ServiceException.INVALID_REQUEST("invalid account email address: " + name, null);
/*     */       }
/*  80 */       a.add(name);
/*     */     }
/*  82 */     return a;
/*     */   }
/*     */   
/*     */   private static List<NamedEntry> getAccountsOnServer() throws ServiceException {
/*  86 */     Provisioning prov = Provisioning.getInstance();
/*  87 */     Server server = prov.getLocalServer();
/*  88 */     String serverName = server.getAttr("zimbraServiceHostname");
/*     */     
/*  90 */     SearchAccountsOptions searchOpts = new SearchAccountsOptions(new String[] { "zimbraId" });
/*     */     
/*  92 */     searchOpts.setSortOpt(SearchDirectoryOptions.SortOpt.SORT_DESCENDING);
/*     */     
/*  94 */     List<NamedEntry> accts = prov.searchAccountsOnServer(server, searchOpts);
/*     */     
/*  96 */     ZimbraLog.calendar.info("Found " + accts.size() + " accounts on server " + serverName);
/*  97 */     return accts;
/*     */   }
/*     */   
/*     */   private static void fixAccounts(List<String> acctNames) throws ServiceException
/*     */   {
/* 102 */     int numAccts = acctNames.size();
/* 103 */     boolean all = (numAccts == 1) && ("all".equals(acctNames.get(0)));
/* 104 */     int numFixedAccts = 0;
/* 105 */     int numFixedAppts = 0;
/*     */     List<NamedEntry> accts;
/* 107 */     List<NamedEntry> accts; if (all) {
/* 108 */       accts = getAccountsOnServer();
/*     */     } else {
/* 110 */       accts = new ArrayList(acctNames.size());
/* 111 */       for (String name : acctNames) {
/*     */         try {
/* 113 */           accts.add(Provisioning.getInstance().get(Key.AccountBy.name, name));
/*     */         } catch (ServiceException e) {
/* 115 */           ZimbraLog.calendar.error("Error looking up account " + name + ": " + e.getMessage(), e);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 120 */     numAccts = accts.size();
/* 121 */     int every = 10;
/* 122 */     for (NamedEntry entry : accts) {
/* 123 */       if ((entry instanceof Account))
/*     */       {
/* 125 */         Account acct = (Account)entry;
/* 126 */         Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
/*     */         try {
/* 128 */           numFixedAppts += mbox.fixAllCalendarItemEndTime(null);
/*     */         } catch (ServiceException e) {
/* 130 */           ZimbraLog.calendar.error("Error fixing calendar item end times in mailbox " + mbox.getId() + ": " + e.getMessage(), e);
/*     */         }
/*     */         
/*     */ 
/* 134 */         numFixedAccts++;
/* 135 */         if (numFixedAccts % every == 0) {
/* 136 */           ZimbraLog.calendar.info("Progress: fixed calendar item end times in " + numFixedAccts + "/" + numAccts + " accounts");
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 141 */     ZimbraLog.calendar.info("Fixed end times in total " + numFixedAppts + " calendar items in " + numFixedAccts + " accounts");
/*     */   }
/*     */   
/*     */   private static class CalendarEndTimeFixupThread extends Thread
/*     */   {
/*     */     private List<String> mAcctNames;
/*     */     
/*     */     public CalendarEndTimeFixupThread(List<String> acctNames) {
/* 149 */       setName("CalendarEndTimeFixupThread");
/* 150 */       this.mAcctNames = acctNames;
/*     */     }
/*     */     
/*     */     public void run() {
/*     */       try {
/* 155 */         FixCalendarEndTime.fixAccounts(this.mAcctNames);
/*     */       } catch (ServiceException e) {
/* 157 */         ZimbraLog.calendar.error("Error while fixing up calendar end times: " + e.getMessage(), e);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public void docRights(List<AdminRight> relatedRights, List<String> notes)
/*     */   {
/* 165 */     notes.add("Only system admins are allowed.");
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/admin/FixCalendarEndTime.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */